package com.sbio.udiapp.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.sbio.udiapp.R;
import com.sbio.udiapp.listener.ListAdapterListener;
import com.sbio.udiapp.listener.ListViewAdapterListener;
import com.sbio.udiapp.util.StringUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;

public class ListViewAdapter<T extends ListItemData> extends RecyclerView.Adapter<ViewHolder1<T>> {

    private List<T> dataList;
    private ListViewAdapterListener<T> listener;

    public ListViewAdapter(List<T> dataList, ListViewAdapterListener<T> listener) {
        this.dataList = dataList;
        this.listener = listener;
    }

    @NonNull
    @Override
    public ViewHolder1 onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_list_view_item, parent, false);
        return new ViewHolder1(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder1<T> holder, int position) {
        T data = dataList.get(position);
        holder.bind(data, listener);
    }

    @Override
    public int getItemCount() {
        return dataList.size();
    }

    // ViewHolder 类 - 不再需要泛型
//    public static class MyViewHolder extends RecyclerView.ViewHolder {
//        TextView tv;
//        Button remove;
//
//        public MyViewHolder(@NonNull View itemView) {
//            super(itemView);
//            tv = itemView.findViewById(R.id.tv_box_code);
//            remove = itemView.findViewById(R.id.remove);
//        }
//
//        // 修改 bind 方法接收 ListItemData 类型
//        public void bind(ListItemData data, ListViewAdapterListener listener) {
//            tv.setText(data.getDisplayText());
//            remove.setOnClickListener(v -> listener.remove(data.getKey()));
//        }
//    }
}